package com.hzx.controller;

import com.hzx.pojo.ComPicture;
import com.hzx.pojo.Commodity;
import com.hzx.pojo.Order;
import com.hzx.service.ComPictureService;
import com.hzx.service.CommodityService;
import com.hzx.service.OrderService;
import com.hzx.service.ShopReleaseService;
import com.hzx.until.FileUpload;
import com.hzx.until.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ModifyDemandController {

    private static String ORDERPICTUREPATH = "F:\\newideafile\\dbpj\\src\\main\\resources\\static\\img\\OrderPicture\\";

    @Autowired
    private ShopReleaseService shopReleaseService;

    @Autowired
    private ComPictureService comPictureService;

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private OrderService orderService;


    public  Integer demandindexid; // 单独一件求购物品的 id

    @GetMapping({"/modifydemand.html","/ha"})
    public String modifyDemandController(Model model, @RequestParam Integer id) {
        demandindexid = id;
        Order order = orderService.selectByid(id);

        model.addAttribute(order);
        return "modifydemand";
    }

    @RequestMapping(value = "/modifydemand", method = RequestMethod.POST)
    public String modifyDemand(@RequestParam String find_name,
                                @RequestParam String price,
                                @RequestParam String take_way,
                                @RequestParam String content,
                                @RequestParam Integer kid,
                                @RequestPart("img") MultipartFile file,
                                HttpServletRequest request,
                                Model model) {

        Integer uid = (Integer) request.getSession().getAttribute("uid");


       Order order = new Order();
       order.setId(demandindexid);
       order.setUid(uid);


        if (!StringUntil.isNullOrEmpty(find_name)) {
            order.setFind_name(find_name);
        }
        if (!StringUntil.isNullOrEmpty(kid)) {
            order.setKid(kid);
        }
        if (!StringUntil.isNullOrEmpty(content)) {

            order.setContent(content);
        }
        if (!StringUntil.isNullOrEmpty(price)) {
            order.setPrice(Double.parseDouble(price));
        }
        if (!StringUntil.isNullOrEmpty(take_way)) {
            order.setTake_way(take_way);
        }


        int ans = orderService.updateByPrimaryKeySelective(order);  // 通过求购的 id 修改 求购信息

        if (ans == 1) {
            model.addAttribute(order);
            return "shopdemand";
        } else {
            return "修改失败";
        }
    }



}
